<Project DefaultTargets="Publish;PublishItemsOutputGroup">
  <!-- This project is specifically to build and publish a build of crossgen2 for shipping. -->
  <PropertyGroup>
    <_IsPublishing>true</_IsPublishing>
  </PropertyGroup>

  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

  <PropertyGroup>
    <NativeAotSupported Condition="$(OutputRID.StartsWith('tizen')) == 'true'">false</NativeAotSupported>
    <NativeAotSupported Condition="$(OutputRID.EndsWith('-arm')) == 'true'">false</NativeAotSupported>
    <NativeAotSupported Condition="$(OutputRID.EndsWith('-x86')) == 'true'">false</NativeAotSupported>
    <!-- Publish crossgen2 as a single-file app on native-OS builds. Cross-OS NativeAOT compilation is not supported yet -->
    <NativeAotSupported Condition="'$(CrossBuild)' == 'true' and '$(TargetOS)' != '$(HostOS)'">false</NativeAotSupported>
    <PublishTrimmed>true</PublishTrimmed>
    <RuntimeIdentifier>$(PackageRID)</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
    <UseLocalAppHostPack>true</UseLocalAppHostPack>
  </PropertyGroup>

  <Import Project="crossgen2.props" />

  <PropertyGroup Condition="'$(NativeAotSupported)' != 'true'">
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>true</PublishReadyToRun>
    <!-- Disable crossgen on NetBSD, illumos, Solaris, and Haiku for now. This can be revisited when we have full support. -->
    <PublishReadyToRun Condition="'$(TargetOS)' == 'netbsd' Or '$(TargetOS)' == 'illumos' Or '$(TargetOS)' == 'solaris' Or '$(TargetOS)' == 'haiku'">false</PublishReadyToRun>
    <!-- Disable crossgen on FreeBSD when cross building from Linux. -->
    <PublishReadyToRun Condition="'$(TargetOS)' == 'freebsd' and '$(CrossBuild)' == 'true'">false</PublishReadyToRun>
    <PublishReadyToRunComposite>$(PublishReadyToRun)</PublishReadyToRunComposite>
  </PropertyGroup>

  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

  <ItemGroup>
    <KnownFrameworkReference Remove="Microsoft.AspNetCore.App" />
    <KnownFrameworkReference Remove="Microsoft.WindowsDesktop.App" />
  </ItemGroup>

  <Import Project="$(RepositoryEngineeringDir)targetingpacks.targets" />
  <Import Project="$(RepositoryEngineeringDir)codeOptimization.targets" />

  <PropertyGroup Condition="'$(NativeAotSupported)' == 'true'">
    <IlcToolsPath>$(CoreCLRILCompilerDir)</IlcToolsPath>
    <IlcToolsPath Condition="'$(CrossBuild)' == 'true' or '$(BuildArchitecture)' != '$(TargetArchitecture)' or '$(EnableNativeSanitizers)' != ''">$(CoreCLRCrossILCompilerDir)</IlcToolsPath>
    <SysRoot Condition="('$(CrossBuild)' == 'true' or '$(BuildArchitecture)' != '$(TargetArchitecture)') and '$(HostOS)' != 'windows'">$(ROOTFS_DIR)</SysRoot>
    <IlcBuildTasksPath>$(CoreCLRILCompilerDir)netstandard/ILCompiler.Build.Tasks.dll</IlcBuildTasksPath>
    <IlcSdkPath>$(CoreCLRAotSdkDir)</IlcSdkPath>
    <IlcFrameworkPath>$(MicrosoftNetCoreAppRuntimePackRidLibTfmDir)</IlcFrameworkPath>
    <IlcFrameworkNativePath>$(MicrosoftNetCoreAppRuntimePackNativeDir)</IlcFrameworkNativePath>
    <TrimmerSingleWarn>false</TrimmerSingleWarn>
    <!-- Use .dwarf files instead of .dsym files since our symbol exporting may not safely handle folders. -->
    <NativeSymbolExt Condition="'$(_IsApplePlatform)' == 'true'">.dwarf</NativeSymbolExt>
    <DsymUtilOptions Condition="'$(_IsApplePlatform)' == 'true'">--flat</DsymUtilOptions>
  </PropertyGroup>

  <ItemGroup Condition="'$(NativeAotSupported)' == 'true'">
    <CustomLinkerArg Condition="'$(CrossBuild)' == 'true' and '$(_hostArchitecture)' == '$(_targetArchitecture)' and '$(_IsApplePlatform)' != 'true' and '$(_hostOS)' != 'windows'" Include="--gcc-toolchain=$(ROOTFS_DIR)/usr" />
  </ItemGroup>

  <Import Project="$(CoreCLRBuildIntegrationDir)Microsoft.DotNet.ILCompiler.SingleEntry.targets"
          Condition="'$(NativeAotSupported)' == 'true'" />
  <Import Project="$(RepositoryEngineeringDir)nativeSanitizers.targets" />

  <!-- Needed for the amd64 -> amd64 musl cross-build to pass the target flag. -->
  <Target Name="_FixIlcTargetTriple"
          AfterTargets="SetupOSSpecificProps"
          Condition="'$(CrossBuild)' == 'true' and '$(HostOS)' != 'windows'">
    <!-- Compute CrossCompileRid, and copy the downstream logic as-is. -->
    <PropertyGroup>
      <CrossCompileRid>$(RuntimeIdentifier)</CrossCompileRid>

      <CrossCompileArch />
      <CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-x64'))">x86_64</CrossCompileArch>
      <CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-arm64')) and '$(_IsApplePlatform)' != 'true'">aarch64</CrossCompileArch>
      <CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-arm64')) and '$(_IsApplePlatform)' == 'true'">arm64</CrossCompileArch>

      <TargetTriple />
      <TargetTriple Condition="'$(CrossCompileArch)' != ''">$(CrossCompileArch)-linux-gnu</TargetTriple>
      <TargetTriple Condition="'$(CrossCompileArch)' != '' and ($(CrossCompileRid.StartsWith('linux-musl')) or $(CrossCompileRid.StartsWith('alpine')))">$(CrossCompileArch)-alpine-linux-musl</TargetTriple>
      <TargetTriple Condition="'$(CrossCompileArch)' != '' and ($(CrossCompileRid.StartsWith('freebsd')))">$(CrossCompileArch)-unknown-freebsd12</TargetTriple>
    </PropertyGroup>

    <ItemGroup>
      <LinkerArg Include="--target=$(TargetTriple)" Condition="'$(_IsApplePlatform)' != 'true' and '$(TargetTriple)' != ''" />
    </ItemGroup>
  </Target>

  <Target Name="LocateNativeCompiler"
          Condition="'$(NativeAotSupported)' == 'true' and '$(HostOS)' != 'windows'"
          BeforeTargets="SetupOSSpecificProps">
      <PropertyGroup>
        <CppCompilerAndLinker Condition="'$(CppCompilerAndLinker)' == ''">clang</CppCompilerAndLinker>
      </PropertyGroup>

      <Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(CppCompilerAndLinker)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo &quot;$CC;$LDFLAGS&quot;' 2>/dev/null"
            EchoOff="true"
            ConsoleToMsBuild="true"
            StandardOutputImportance="Low">
        <Output TaskParameter="ConsoleOutput" PropertyName="_CC_LDFLAGS" />
      </Exec>

    <PropertyGroup>
      <CppLinker>$(_CC_LDFLAGS.SubString(0, $(_CC_LDFLAGS.IndexOf(';'))))</CppLinker>
      <_LDFLAGS>$(_CC_LDFLAGS.SubString($([MSBuild]::Add($(_CC_LDFLAGS.IndexOf(';')), 1))))</_LDFLAGS>
      <LinkerFlavor Condition="$(_LDFLAGS.Contains('lld'))">lld</LinkerFlavor>
    </PropertyGroup>
  </Target>

</Project>
